﻿using Aspose.Cells;
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Excel生成二维码
{
    internal class Program
    {
        static void Main(string[] args)
        {
            if (DateTime.Now > new DateTime(2025, 1, 1))
            {
                Environment.Exit(0);
            }
            Console.WriteLine("请输入Excel的路径");
            string path = Console.ReadLine().Replace("\"", "");

            using (Workbook book = new Workbook(path))
            {
                using (Worksheet sh = book.Worksheets[0])
                {
                    Cells cells = sh.Cells;
                    int rowCount = cells.MaxDataRow + 1;
                    for (int row = 0; row < rowCount; row++)
                    {
                        using (Stream s = QrCodeOperation(cells[row].Value.ToString()))
                        {
                            sh.Pictures.Add(row, 1, s);
                        }
                    }

                    book.Save(path);

                }

            }
        }


        public static Stream QrCodeOperation(string txt)
        {
            MemoryStream ms = new MemoryStream();
            using (QRCodeGenerator qrCodeGenerator = new QRCodeGenerator())
            {
                byte[] b = Encoding.UTF8.GetBytes(txt);
                using (QRCodeData data = qrCodeGenerator.CreateQrCode(b, QRCodeGenerator.ECCLevel.H))
                {
                    using (QRCode qr = new QRCode(data))
                    {
                        qr.GetGraphic(10, System.Drawing.Color.Black, System.Drawing.Color.White, true).Save(ms, ImageFormat.Jpeg);

                        return ms;
                    }
                }

            }
        }
    }
}
